package com.etool.modules.auth.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.etool.modules.auth.model.AuthMenu;
import com.etool.modules.curd.service.CurdService;

@Component
public class AuthService {
	@Resource
	private CurdService curdService;
	
	@Resource
	private AuthMenuService authMenuService;
	
	public Map<String,Object> cache=new HashMap<String, Object>();

	public List<AuthMenu> auths(int workerId)
	{
		String key="auth:{workerId:"+workerId+"}";
		
		if(cache.get(key)!=null)
		{
			return (List<AuthMenu>)cache.get(key);
		}
		
		
		List<AuthMenu> menus=new ArrayList<AuthMenu>();
		
		// 用户与角色对应数据
		List<Map<String, Object>> workerRoles= curdService.read("auth_worker_role", Collections.singletonMap("workerId",(Object)workerId), 0, 1,null);
		
		if(workerRoles==null || workerRoles.size()!=1)
		{
			return Collections.emptyList();
		}
		
		int roleId=(Integer)workerRoles.get(0).get("roleId");
		
		
		// 角色于菜单对应数据
		List<Map<String, Object>> roleMenuS=curdService.read("auth_role_menu", Collections.singletonMap("roleId",(Object)roleId));
		
		for(Map<String, Object> item:roleMenuS)
		{
			int menuId=(Integer)item.get("menuId");
			AuthMenu menu=authMenuService.cacheSelectById(menuId);
			menus.add(menu);
		}
		
		cache.put(key,menus);
		
		return menus;
	}
	
	
	public void setCurdService(CurdService curdService) {
		this.curdService = curdService;
	}


	public void setAuthMenuService(AuthMenuService authMenuService) {
		this.authMenuService = authMenuService;
	}
	
	
}
